﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using StockholmsStad.Windows.UI.Modules.Couple.Models;
using System.Windows.Input;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Infrastructure.Controls;

namespace StockholmsStad.Windows.UI.Modules.Couple.Controls
{
    /// <summary>
    /// Interaction logic for Par.xaml
    /// </summary>
    public partial class CoupleDetailsControl
    {
        //private bool isInChangeMode;
        private bool initiallyLoaded;
        private CoupleDetailPresentationModel model;

        public CoupleDetailsControl()
        {
            InitializeComponent();
            DataContextChanged += CoupleDetailsControl_DataContextChanged;
            Loaded += CoupleDetailsControl_Loaded;

            PreviewLostKeyboardFocus += CoupleDetailsControl_PreviewLostKeyboardFocus;
            //InitChangeMode();
        }

        private void InitChangeMode()
        {
            ContactFirstNameTextBox.TextChanged += UpdateTextChangeMode;
            ContactLastNameTextBox.TextChanged += UpdateTextChangeMode;
            PhoneTextBox.TextChanged += UpdateTextChangeMode;
            EmailTextBox.TextChanged += UpdateTextChangeMode;
            Person2LastNameTextBox.TextChanged += UpdateTextChangeMode;
            Person2FirstNamesTextBox.TextChanged += UpdateTextChangeMode;
            Person2PersonalNumberTextBox.TextChanged += UpdateTextChangeMode;
            Person2NationalityTextBox.TextChanged += UpdateTextChangeMode;
            NationalityTextBox.TextChanged += UpdateTextChangeMode;
            CityTextBox.TextChanged += UpdateTextChangeMode;
            ZipCodeTextBox.TextChanged += UpdateTextChangeMode;
            StreetAddressTextBox.TextChanged += UpdateTextChangeMode;
            CoAddressTextBox.TextChanged += UpdateTextChangeMode;
            Person1LastNameTextBox.TextChanged += UpdateTextChangeMode;
            Person1FirstNamesTextBox.TextChanged += UpdateTextChangeMode;
            rbMembershipPerson1No.Checked += UpdateCheckedChangeMode;
            rbMembershipPerson1Yes.Checked += UpdateCheckedChangeMode;
            rbMembershipPerson2No.Checked += UpdateCheckedChangeMode;
            rbMembershipPerson2Yes.Checked += UpdateCheckedChangeMode;
            rbPerson1Female.Checked += UpdateCheckedChangeMode;
            rbPerson1Male.Checked += UpdateCheckedChangeMode;
            rbPerson2Female.Checked += UpdateCheckedChangeMode;
            rbPerson2Male.Checked += UpdateCheckedChangeMode;
            RadioButtonPerson1.Checked += UpdateCheckedChangeMode;
            RadioButtonOtherPerson.Checked += UpdateCheckedChangeMode;
            Person2ProtectedIdentityCheckBox.Checked += UpdateCheckedChangeMode;
        }

        void UpdateCheckedChangeMode(object sender, RoutedEventArgs e)
        {
            UpdateChangeMode();
        }

        private void UpdateTextChangeMode(object sender, TextChangedEventArgs e)
        {
            UpdateChangeMode();
        }

        private void UpdateChangeMode()
        {
            if (model != null && initiallyLoaded) model.IsInChangeMode = true;
        }
        private void CoupleDetailsControl_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var newfocus = e.NewFocus;
            if (newfocus.GetType() == typeof(TabItem))
            {
                var tabItem = (TabItem)newfocus;
                if (!tabItem.Header.ToString().Equals(HeaderNames.Couple))
                {
                    //Byte till annan tab
                    e.Handled = HandleChange();
                }
            }
            if (newfocus.GetType() == typeof(ListViewItem)) 
            {
                var listViewItem = (ListViewItem) newfocus;
                if (listViewItem.Content != null && 
                    listViewItem.Content is CouplesSummaryItem)
                {
                    //Assume it's the couples list view 
                    e.Handled = HandleChange();
                }
            }
        }

        private bool HandleChange()
        {
            if (model.IsInChangeMode)
            {
                var result = MessageBox.Show(Properties.Resources.UnsavedChanges, "Information", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.Yes)
                {
                    ToggleMode(false);
                }
                else
                {
                    return true;
                }
            }
            return false;
        }

        private void CoupleDetailsControl_Loaded(object sender, RoutedEventArgs e)
        {
            
            //We need to set this manually because of extendedradiobutton has a difference between IsChecked and IsCheckedReal
            //when we change couple on another tab and then change to this tab
            if (IsFalse(rbMembershipPerson1Yes) && IsFalse(rbMembershipPerson1No))
            {
                rbMembershipPerson1Yes.IsChecked = model.Person1SwedishNationality;
                rbMembershipPerson1No.IsChecked = !model.Person1SwedishNationality;
            }
            if (IsFalse(rbMembershipPerson2Yes) && IsFalse(rbMembershipPerson2No))
            {
                rbMembershipPerson2Yes.IsChecked = model.Person2SwedishNationality;
                rbMembershipPerson2No.IsChecked = !model.Person2SwedishNationality;
            }
            if (IsFalse(rbPerson1Female) && IsFalse(rbPerson1Male))
            {
                rbPerson1Male.IsChecked = model.Person1SexMale;
                rbPerson1Female.IsChecked = !model.Person1SexMale;
            }
            if (IsFalse(rbPerson2Female) && IsFalse(rbPerson2Male))
            {
                rbPerson2Male.IsChecked = model.Person2SexMale;
                rbPerson2Female.IsChecked = !model.Person2SexMale;
            }
            if (IsFalse(RadioButtonPerson1) && IsFalse(RadioButtonOtherPerson))
            {
                RadioButtonPerson1.IsChecked = RadioButtonPerson1.IsCheckedReal;
                RadioButtonOtherPerson.IsChecked = RadioButtonOtherPerson.IsCheckedReal;
            }

            model.IsInChangeMode = false;
            InitChangeMode();
            initiallyLoaded = true;
            
            
        }

        private static bool IsFalse(ToggleButton radioButton)
        {
            if (radioButton != null && radioButton.IsChecked.HasValue)
                return (bool)(!radioButton.IsChecked);

            return true;
        }

        private void CoupleDetailsControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = DataContext as CoupleDetailPresentationModel;

            if(model == null)
                return;

            //bool enabled = model.TicketId == null ? true : false;
            //bool enabled = true;
            //ToggleMode(enabled);
        }

        private void Person1ProtectedIdentityCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var chBox = sender as CheckBox;

            if (chBox != null)
                if (chBox.IsChecked.HasValue && chBox.IsChecked.Value)
                {
                    Person1PersonalNumberTextBox.Text = model.Person1PersonalNumber = string.Empty;
                    Person1PersonalNumberTextBox.IsEnabled = false;
                }
                else
                {
                    Person1PersonalNumberTextBox.IsEnabled = true;
                }
        }

        private void Person2ProtectedIdentityCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var chBox = sender as CheckBox;

            if (chBox != null)
                if (chBox.IsChecked.HasValue && chBox.IsChecked.Value)
                {
                    Person2PersonalNumberTextBox.Text = model.Person2PersonalNumber = string.Empty;
                    Person2PersonalNumberTextBox.IsEnabled = false;
                }
                else
                {
                    Person2PersonalNumberTextBox.IsEnabled = true;
                }
        }

        private void RadioButtonContact_Checked(object sender, RoutedEventArgs e)
        {
            if(model == null)
                return;

            if(RadioButtonPerson1.IsChecked.HasValue && RadioButtonPerson1.IsChecked.Value)
            {
                ContactFirstNameTextBox.Text = model.Person1FirstNames;
                ContactLastNameTextBox.Text = model.Person1LastName;
            }

            if (RadioButtonOtherPerson.IsChecked.HasValue && RadioButtonOtherPerson.IsChecked.Value)
            {
                //var contactIsPerson1 = 
                //        (model.ContactPersonFirstName == ContactFirstNameTextBox.Text)
                //        &&
                //        (model.ContactPersonLastName == ContactLastNameTextBox.Text);

                var contactIsPerson1 =
                        (model.Person1FirstNames == ContactFirstNameTextBox.Text)
                        &&
                        (model.Person1LastName == ContactLastNameTextBox.Text);

                if (contactIsPerson1)
                {
                    model.ContactPersonFirstName = ContactFirstNameTextBox.Text = string.Empty;
                    model.ContactPersonLastName = ContactLastNameTextBox.Text = string.Empty;
                    model.Phone = PhoneTextBox.Text = string.Empty;
                    model.Email = EmailTextBox.Text = string.Empty;
                }
            }


        }

        //private void rbMembershipPerson1Yes_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (model != null && rbMembershipPerson1Yes.IsChecked.HasValue)
        //        model.Person1SwedishNationality = rbMembershipPerson1Yes.IsChecked.Value;
        //}

        //private void rbMembershipPerson1No_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (model != null && rbMembershipPerson1No.IsChecked.HasValue)
        //        model.Person1SwedishNationality = !rbMembershipPerson1No.IsChecked.Value;
        //}

        //private void rbMembershipPerson2No_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (model != null && rbMembershipPerson2No.IsChecked.HasValue)
        //        model.Person2SwedishNationality = !rbMembershipPerson2No.IsChecked.Value;
        //}

        //private void rbMembershipPerson2Yes_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (model != null && rbMembershipPerson2Yes.IsChecked.HasValue)
        //        model.Person2SwedishNationality = rbMembershipPerson2Yes.IsChecked.Value;
        //}

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;
            //ToggleMode(false);
        }

        private void ChangeButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleMode(true);
        }

        private void ToggleMode(bool enabled)
        {
            model.IsInChangeMode = enabled;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleMode(true);
        }
    }
}
